//
//  StudyingViewController.m
//  Lazy
//
//  Created by Echo on 14-4-1.
//  Copyright (c) 2014年 Echo. All rights reserved.
//

#import "StudyingViewController.h"


@interface StudyingViewController ()

@end

@implementation StudyingViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}
-(void)dealloc{
    
    endsecs = [[NSDate date] timeIntervalSince1970];
    NSDateFormatter *formatter	=  [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"HH:mm"];
    info.endTime = [formatter stringFromDate:[NSDate date]];
    info.seconds = [NSString stringWithFormat:@"%f", endsecs-cursecs];
    
    [curArray addObject:info];
    
    NSString *path = [RecInfo storePathWithFileName:RECINFO];
    [RecInfo archiverObject:curArray path:path key:RECINFO];
    if (isDefalut) {
        [NC postNotificationName:NOTI_END_DEFALUT_CURE object:nil];
    }else{
        [NC postNotificationName:NOTI_END_CURE object:nil];
    }
}
- (void)viewDidLoad
{
//    [DataManager archiverObject:self.deviceArray path:self.devicePath key:self.deviceName];
    
    [super viewDidLoad];
    
    curArray = [[NSMutableArray alloc] init];
    NSString *path = [RecInfo storePathWithFileName:RECINFO];
    if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
        NSArray *array =  [RecInfo unarchiverObjectFromPath:path key:RECINFO];
        [curArray addObjectsFromArray:array];
    }
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = STR(@"治疗中");
    UIImageView *bg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64)];
    bg.image = IMG(@"studying.png");
    [self.view addSubview:bg];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
    view.backgroundColor = RGB(246, 246, 246, 1);
    [self.view addSubview:view];
    
    info = [[RecInfo alloc] init];
    NSDateFormatter *formatter	=  [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy/MM/dd"];//HH:mm
    NSString  *strdate = [formatter stringFromDate:[NSDate date]];
    info.day = strdate;
    [formatter setDateFormat:@"HH:mm"];
    info.startTime = [formatter stringFromDate:[NSDate date]];
    
    cursecs = [[NSDate date] timeIntervalSince1970];
    
    imageviewModel = [[UIImageView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT-348/2-64, SCREEN_WIDTH, 348/2)];
    [self.view addSubview:imageviewModel];
    NSString *string = [NSString stringWithFormat:@"模式%d.png", nModel+1];
    imageviewModel.image = IMG(string);
    
    for (int i=0; i<3; i++) {
        string = [NSString stringWithFormat:@"强度%d.png", nLevel];
        UIImageView *imageviewLevel = [[UIImageView alloc] initWithImage:IMG(string)];
        imageviewLevel.frame = CGRectMake(5+i*50, SCREEN_HEIGHT-FH(imageviewLevel)-64, FW(imageviewLevel), FH(imageviewLevel)*2);
        if (i == 2) {
            imageviewLevel.frame = CGRectMake(SCREEN_WIDTH-FW(imageviewLevel)-5, FY(imageviewLevel), FW(imageviewLevel), FH(imageviewLevel));
        }else if (i == 1) {
            imageviewLevel.frame = CGRectMake((SCREEN_WIDTH-FW(imageviewLevel))/2, FY(imageviewLevel), FW(imageviewLevel), FH(imageviewLevel));
        }
        [self.view addSubview:imageviewLevel];
    }
    
    UIImageView *imageviewbg = [[UIImageView alloc] initWithImage:IMG(@"user_bg.png")];
    imageviewbg.frame = CGRectMake((SCREEN_WIDTH-FW(imageviewbg))/2, 55, FW(imageviewbg), FH(imageviewbg));
    [self.view addSubview:imageviewbg];
    
    UIImageView *imageview = [[UIImageView alloc] initWithImage:IMG(@"user.png")];
    imageview.frame = CGRectMake((FW(imageviewbg)-FW(imageview))/2, 20, FW(imageview), FH(imageview));
    [imageviewbg addSubview:imageview];
    
    for (int i=0; i<2; i++) {
        UIImageView *imageviewEar = [[UIImageView alloc] init];
        if (i == 0) {
            imageviewEar.image = IMG(@"左1.png");
            imageviewEar.animationImages=[NSArray arrayWithObjects:
                                          [UIImage imageNamed:@"左1.png"],
                                          [UIImage imageNamed:@"左2.png"],
                                          nil];
            imageviewEar.animationDuration = 1;
            [imageviewEar startAnimating];
            if (nEar == 2){
                imageviewEar.hidden = YES;
            }
        }else{
            imageviewEar.image = IMG(@"右1.png");
            imageviewEar.animationImages=[NSArray arrayWithObjects:
                                          [UIImage imageNamed:@"右1.png"],
                                          [UIImage imageNamed:@"右2.png"],
                                          nil];
            imageviewEar.animationDuration = 1;
            [imageviewEar startAnimating];
            if (nEar == 1){
                imageviewEar.hidden = YES;
            }
        }
        imageviewEar.frame = CGRectMake(72+i*146, 105, 32, 32);
        [self.view addSubview:imageviewEar];
    }
    
    for (int i=0; i<2; i++) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, SCREEN_HEIGHT-100-64-40+i*19, 100, 20)];
        label.textColor = [UIColor whiteColor];
        label.font = [UIFont systemFontOfSize:16];
        label.text = STR(@"强度");
        if (i==1) {
            label.font = [UIFont systemFontOfSize:14];
            label.text = STR(@"模式");
        }
        label.backgroundColor = [UIColor clearColor];
        [self.view addSubview:label];
    }
    
}
-(void)setLevel:(NSString*)level{
    if (level) {
        nLevel = [level intValue];
    }else{
        nLevel = 1;
    }
}
-(void)setModel:(NSString*)model{
    if (model) {
        nModel = [model intValue];
    }else{
        nModel = 1;
    }
}
-(void)setPosition:(NSString*)pos{
    nEar = [pos intValue];
}
-(void)setDefaultMode{
    isDefalut = YES;
}

@end

















